<template>
  <div class="container">
    <div class="body">
      <el-form
        ref="createTransferValue"
        :model="createTransferValue"
        label-width="120px"
      >
        <el-form-item :label="createTransferRemark.fromId">
          <el-select
            v-model="createTransferValue.fromId"
            placeholder="please select your account"
          >
            <el-option
              v-for="item in createTransferRemark.fromIdOptions"
              :key="item.accountId"
              :label="item.accountId"
              :value="item.accountId"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="createTransferRemark.toId">
          <el-input v-model="createTransferValue.toId" />
        </el-form-item>
        <el-form-item :label="createTransferRemark.type">
          <el-select
            v-model="createTransferValue.type"
            placeholder="please select your Transfer type"
          >
            <el-option
              v-for="item in createTransferRemark.typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="createTransferRemark.money">
          <el-input v-model="createTransferValue.money" />
        </el-form-item>
        <el-form-item :label="createTransferRemark.serviceCharge">
          <el-input v-model="createTransferValue.serviceCharge" />
        </el-form-item>
        <el-form-item>
          <el-button @click="onCancel">取消</el-button>
          <el-button type="primary" @click="createTransfer">创建</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  created() {
    this.getAccounts();
  },
  data() {
    return {
      createTransferValue: {
        fromId: "",
        toId: "",
        type: "",
        money: "",
        serviceCharge: "0.1",
        remarks: "",
        createdTime: "",
        url: "/basic/transfer/add",
      },
      createTransferRemark: {
        title: "展开转账",
        fromId: "转账方",
        fromIdOptions: [],
        toId: "接收方",
        type: "转账类型",
        typeOptions: [
          {
            label: "实时转账",
            value: "RealTimeTransfer",
          },
          {
            label: "两小时转账",
            value: "TwoHourTransfer",
          },
          {
            label: "一天转账",
            value: "OnedayTransfer",
          },
        ],
        money: "总额",
        serviceCharge: "手续费",
      },
    };
  },
  methods: {
    getAccounts() {
      this.$store
        .dispatch("request/get", "/user/account/query")
        .then((data) => {
          this.createTransferRemark.fromIdOptions = data;
        });
    },
    onSubmit() {
      this.$message("submit!");
    },
    onCancel() {
      this.$message({
        message: "cancel!",
        type: "warning",
      });
    },
    createTransfer() {
      this.createTransferRemark.fromIdOptions.forEach((item) => {
        if (item.accountId === this.createTransferValue.fromId) {
          if (Number(item.money) < Number(this.createTransferValue.money)) {
            this.$message.error("余额不足！！");
            return false;
          } else {
            this.$store
              .dispatch("request/post", this.createTransferValue)
              .then(() => {
                this.$message.success("转账成功!!");
                this.createTransferValue = "";
              });
          }
        }
      });
    },
  },
};
</script>

<style scoped>
.container {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: conter;
  align-content: center;
}
.body {
  height: 50%;
  width: 50%;
  display: flex;
  justify-content: center;
  align-items: conter;
}
</style>

